package com.ruoyi.product.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.db.domain.ProductInfo;
import com.ruoyi.db.domain.ProductPush;
import com.ruoyi.db.mapper.ProductInfoMapper;
import com.ruoyi.db.mapper.ProductPushMapper;
import com.ruoyi.market.constant.RedisKeyConstent;
import com.ruoyi.product.service.IProductPushService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 商品上架管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-07-31
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class ProductPushServiceImpl implements IProductPushService 
{
    @Resource
    private ProductPushMapper productPushMapper;
    @Resource
    private ProductInfoMapper productInfoMapper;
    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 查询商品上架管理
     * 
     * @param id 商品上架管理主键
     * @return 商品上架管理
     */
    @Override
    public ProductPush selectProductPushById(Long id)
    {

        return productPushMapper.selectProductPushById(id);
    }

    /**
     * 查询商品上架管理列表
     * 
     * @param productPush 商品上架管理
     * @return 商品上架管理
     */
    @Override
    public List<ProductPush> selectProductPushList(ProductPush productPush)
    {
        return productPushMapper.selectProductPushList(productPush);
    }

    /**
     * 查询商品上架管理列表
     *
     * @param productPush 商品上架管理
     * @return 商品上架管理
     */
    @Override
    public List<Map<String, Object>> selectProductPushListMap(ProductPush productPush)
    {
        return productPushMapper.selectProductPushListMap(productPush);
    }

    /**
     * 新增商品上架管理
     * 
     * @param productPush 商品上架管理
     * @return 结果
     */
    @Override
    public int insertProductPush(ProductPush productPush)
    {

        String notShowCitys = productPush.getNotShowCitys();
        if(StringUtils.isNotEmpty(notShowCitys)){
            String[] split =notShowCitys.split(",");
            List<String>list=JSON.parseArray(JSON.toJSONString(split),String.class);
            productPush.setNotShowCitys(JSON.toJSONString(list));
        }else {
            productPush.setNotShowCitys(JSON.toJSONString(new ArrayList<>()));
        }

        String clientTypes = productPush.getClientTypes();
        if (StringUtils.isNotEmpty(clientTypes)){
            String[] split = clientTypes.split(",");
            List<String> list = JSON.parseArray(JSON.toJSONString(split), String.class);
            productPush.setClientTypes(JSON.toJSONString(list));
        }else{
            productPush.setClientTypes(JSON.toJSONString(new ArrayList<>()));
        }

        String sceneIds = productPush.getSceneIds();
        if (StringUtils.isNotEmpty(sceneIds)){
            String[] split = sceneIds.split(",");
            List<Integer> list = JSON.parseArray(JSON.toJSONString(split), Integer.class);
            productPush.setSceneIds(JSON.toJSONString(list));
        }else{
            productPush.setSceneIds(JSON.toJSONString(new ArrayList<>()));
        }

        String channelTypes = productPush.getChannelTypes();
        if (StringUtils.isNotEmpty(channelTypes)){
            String[] split = channelTypes.split(",");
            List<String> list = JSON.parseArray(JSON.toJSONString(split), String.class);
            productPush.setChannelTypes(JSON.toJSONString(list));
        }else{
            productPush.setChannelTypes(JSON.toJSONString(new ArrayList<>()));
        }

        String zhiMaTypes = productPush.getZhimaFilter();
        if (StringUtils.isNotEmpty(zhiMaTypes)){
            String[] split = zhiMaTypes.split(",");
            List<String> list = JSON.parseArray(JSON.toJSONString(split), String.class);
            productPush.setZhimaFilter(JSON.toJSONString(list));
        }else{
            productPush.setZhimaFilter(JSON.toJSONString(new ArrayList<>()));
        }

        String noShowTypes = productPush.getNotExcludeBrowser();
        if (StringUtils.isNotEmpty(noShowTypes)){
            String[] split = noShowTypes.split(",");
            List<String> list = JSON.parseArray(JSON.toJSONString(split), String.class);
            productPush.setNotExcludeBrowser(JSON.toJSONString(list));
        }else{
            productPush.setNotExcludeBrowser(JSON.toJSONString(new ArrayList<>()));
        }

        ProductInfo productInfo = productInfoMapper.selectProductInfoById(productPush.getProductId());
        productPush.setProductName(productInfo.getProductName());
        productPush.setProductLogo(productInfo.getLogoUrl());
        productPush.setProductPrice(productInfo.getPriceAmount());

        productPush.setCreateTime(DateUtils.getNowDate());
        return productPushMapper.insertProductPush(productPush);
    }

    /**
     * 修改商品上架管理
     * 
     * @param productPush 商品上架管理
     * @return 结果
     */
    @Override
    public int updateProductPush(ProductPush productPush)
    {
        String notShowCitys = productPush.getNotShowCitys();
        if(StringUtils.isNotEmpty(notShowCitys)){
            String[] split =notShowCitys.split(",");
            List<String>list=JSON.parseArray(JSON.toJSONString(split),String.class);
            productPush.setNotShowCitys(JSON.toJSONString(list));
        }else {
            productPush.setNotShowCitys(JSON.toJSONString(new ArrayList<>()));
        }

        String clientTypes = productPush.getClientTypes();
        if (StringUtils.isNotEmpty(clientTypes)){
            String[] split = clientTypes.split(",");
            List<String> list = JSON.parseArray(JSON.toJSONString(split), String.class);
            productPush.setClientTypes(JSON.toJSONString(list));
        }else{
            productPush.setClientTypes(JSON.toJSONString(new ArrayList<>()));
        }

        String sceneIds = productPush.getSceneIds();
        if (StringUtils.isNotEmpty(sceneIds)){
            String[] split = sceneIds.split(",");
            List<Integer> list = JSON.parseArray(JSON.toJSONString(split), Integer.class);
            productPush.setSceneIds(JSON.toJSONString(list));
        }else{
            productPush.setSceneIds(JSON.toJSONString(new ArrayList<>()));
        }

        String channelTypes = productPush.getChannelTypes();
        if (StringUtils.isNotEmpty(channelTypes)){
            String[] split = channelTypes.split(",");
            List<String> list = JSON.parseArray(JSON.toJSONString(split), String.class);
            productPush.setChannelTypes(JSON.toJSONString(list));
        }else{
            productPush.setChannelTypes(JSON.toJSONString(new ArrayList<>()));
        }

        String zhiMaTypes = productPush.getZhimaFilter();
        if (StringUtils.isNotEmpty(zhiMaTypes)){
            String[] split = zhiMaTypes.split(",");
            List<String> list = JSON.parseArray(JSON.toJSONString(split), String.class);
            productPush.setZhimaFilter(JSON.toJSONString(list));
        }else{
            productPush.setZhimaFilter(JSON.toJSONString(new ArrayList<>()));
        }
        String noShowTypes = productPush.getNotExcludeBrowser();
        if (StringUtils.isNotEmpty(noShowTypes)){
            String[] split = noShowTypes.split(",");
            List<String> list = JSON.parseArray(JSON.toJSONString(split), String.class);
            productPush.setNotExcludeBrowser(JSON.toJSONString(list));
        }else{
            productPush.setNotExcludeBrowser(JSON.toJSONString(new ArrayList<>()));
        }

        productPush.setUpdateTime(DateUtils.getNowDate());

        int i = productPushMapper.updateProductPush(productPush);

        String nowDate = LocalDate.now().toString();
        String redisKey = StrUtil.format(RedisKeyConstent.PRODUCT_DAY_UV_COUNT_KEY, nowDate, productPush.getProductId());
        if (i>0){
            redisTemplate.delete(redisKey);
        }

        return i;
    }

    /**
     * 批量删除商品上架管理
     * 
     * @param ids 需要删除的商品上架管理主键
     * @return 结果
     */
    @Override
    public int deleteProductPushByIds(String ids)
    {
        return productPushMapper.deleteProductPushByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除商品上架管理信息
     * 
     * @param id 商品上架管理主键
     * @return 结果
     */
    @Override
    public int deleteProductPushById(Long id)
    {
        return productPushMapper.deleteProductPushById(id);
    }

    @Override
    public boolean changeStatus(ProductPush productPush) {
        return productPushMapper.updateProductPush(productPush) > 0;
    }

    @Override
    public List<Map<String, Object>> listOnLineMap(ProductPush productPush) {
        return productPushMapper.listOnLineMap(productPush);
    }
}
